<?php
namespace Company\Controller;
use Think\Controller;
class ProductorderController extends BaseController {
	public function _initialize()
    {
        parent::_initialize();
        $g=session('agent');
		C('erp_dbname',$g['db']);
    }
	//打印
	public function printproduct(){
			$id=I('id');
			$cid=I('cid');
			
			$ProductOrderDetail = M('ProductOrderDetail',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ProductOrder=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Section=M('Section',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Personnel=M('Personnel',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Unit=M('Unit',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ArrivalOrder=M('ArrivalOrder',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Warehouse=M('Warehouse');
			
			$country = $country_model->select();
			
			$detail=$ProductOrder->where('id='.$id)->find();
			$detail['section_name']=$Section->where(array('no'=>$detail['section_no']))->find();//需求部门
			$detail['people_purchasing_name']=$Personnel->where(array('no'=>$detail['people_purchasing_no']))->find();//申请人
			$detail['people_make_name']=$Personnel->where(array('no'=>$detail['people_make_no']))->find();//制单人
			$detail['unit_name']=$Unit->where(array('unit_no'=>$detail['unit1']))->find();//主单位名称
			$inbound_warehouse_info=$Warehouse->where(array('warehouse_code'=>$detail['inbound_warehouse_code']))->find();
			$detail['inbount_warehouse_name']=$inbound_warehouse_info['name'];
			// dump($detail);
			// $listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['plan_id']=$id;
			$map['status']=array('neq',-1);
			$count=$ProductOrderDetail->where($map)->count();
			$list   = $ProductOrderDetail->where($map)->order('id desc')->select();
			
			// $Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			// $show    = $Page->show();// 分页显示输出
			$count_price=0;//合计总金额
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
				
				$list[$k]['outbound_warehouse_name']=$Warehouse->where(array('warehouse_code'=>$v['outbound_warehouse_code']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				
				$list[$k]['goods_info']=$ArrivalOrder->field('goods_no,goods_name,goods_spec,unit1,price,batch_no')->where(array('id'=>$v['goods_id']))->find();
				
				$list[$k]['goods_info']['unit_name']=$Unit->where(array('unit_no'=>$list[$k]['goods_info']['unit1']))->find();//商品主单位名称
				// echo M('Unit',C('DB_PRS'),C('DB_DEPOT'))->getlastsql();
				$count_price+=$list[$k]['goods_info']['price']*$v['weight'];
            }
			// dump($detail);
			// $this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);

			$this->assign("count_price",$count_price);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
			if($cid==1){//生产计划单
				$this->display('print_examinedetail');
			}
			if($cid==2){//领料单
				$this->display('print_examinedetailpicking');
			}
			if($cid==3){//转换单
				$this->display('print_examinedetailchange');
			}
	}
	//已推送产品列表(回执列表)
	public function receive_list(){
		$keywords=I('get.keywords');     
		$selval=I('get.selval');
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$Product_model=M('ProductOrder',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Byproduct_model=M('ProductOrderByproduct',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Section_model=M('Section',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Personnel_model=M('Personnel',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));

		$map['push_status']=1;
		
		if($selval=='1'){
			$map['purchasing_no'] = array('like','%'.$keywords.'%');
		}

		$count=$Product_model->where($map)->count();
		$list=$Product_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();

		foreach($list as $k=>$v){
			$list[$k]['section_name']=$Section_model->where(array('no'=>$v['section_no']))->find();
			if($v['act_count']==2){
				$by_list=$Byproduct_model->where(array('order_id'=>$v['id']))->select();
				foreach($by_list as $ks=>$vs){
					if($vs['act_count']==2){
						continue;
					}else{
						$list[$k]['noconfirm_receive']=1;//未完成校检
					}
				}
			}else{
				$list[$k]['noconfirm_receive']=1;//未完成校检
			}
			$list[$k]['people_make_name']=$Personnel_model->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=$Personnel_model->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
	//添加副产品
	public function byproduct(){
			$this->tslist();
			$m=M('UserWarehouse');
			$Warehouse=M('Warehouse');
			$warehouse_info=$m->where(array('user_id'=>session('agent.id')))->find();
			
			$warehouse_list=explode(',',$warehouse_info['warehouse_group_id']);

			foreach($warehouse_list as $k=>$v){
				$warehouse_list[$k]=$Warehouse->where(array('id'=>$v))->find();
			}
			$this->assign('warehouse_list',$warehouse_list);
			
			$id=I('id');
			$Productorderdetail_model=M('ProductOrderDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model=M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model=M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));

			$unit_model=M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$b=M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			
			$country = $country_model->select();
			
			$detail=$Product_model->where('id='.$id)->find();
			$byproduct_list=$b->where(array('order_id'=>$id))->select();
			
			$this->assign("byproduct_list",$byproduct_list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
            $this->display();
	}
	//添加副产品
	public function add_byproduct(){
		$unit_model =  M('Unit');
		$unit_list	=	$unit_model->select();
		$this->assign('unit_list',$unit_list);
		if(IS_POST){
			$b=M("ProductOrderByproduct", C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));

			$pro_name=trim(I('pro_name'));
			$pro_spec=trim(I('pro_spec'));
			$source_no=trim(I('source_no'));
			$pro_no=trim(I('pro_no'));

			$map['pro_no']=$pro_no;
			$status=$b->where($map)->find();
			if($status){
				$this->error('产品编码重复');
			}
			$data['source_no']=$source_no;
			$data['pro_name']=$pro_name;
			$data['pro_spec']=$pro_spec;
			$data['pro_no']=$pro_no;
			$data['pro_num_plan']=I('pro_num_plan');
			$data['unit1']=I('unit1');
			$data['order_id']=I('id');
			$data['product_weeks']=I('product_weeks');
			$data['product_marks']=I('product_marks');
			$data['mark']=I('mark');

			$res=$b->add($data);
            if($res){
				$this->success('添加成功');
			}
		}else{
			
			$this->display();
		}
	}
	//入库单参数
	public function inbound_warehouse(){
		$m=M('ProductOrder',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('ProductOrderDetail',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$b=M('ProductOrderByproduct',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));//副产品
		
		$ids=I('ids');
		$g=session('agent');
		$product_list=$m->where(array('id'=>array('in',$ids)))->select();
		$suc_count=0;
        $failed_count=0;
		foreach($product_list as $k=>$v){
			$product_detail_list=$n->where(array('plan_id'=>$v['id']))->select();
			$product_byproduct_list=$b->where(array('order_id'=>$v['id']))->select();
			$product_list[$k]['detail_list']=$product_detail_list;
			$product_list[$k]['byproduct_list']=$product_byproduct_list;
			$d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id")->where(C('DB_PREFIX')."warehouse.warehouse_code='".$v['inbound_warehouse_code']."'")->getField('vars,url,warehouse_code');
			$url=$d['var_9']['url'];
			$warehouse_code=$d['var_9']['warehouse_code'];
			$product_list[$k]['warehouse_code']=$warehouse_code;
			$product_list[$k]['erp_code']=$g['company_id'];
			// dump($url);
			// dump($product_list);
			// die;
			$true=push($url,$product_list);//推送过去
			$true=json_decode($true,'true');
			
			if($true['status']==1){
				$suc_count++;
				$m->where(array('id'=>$v['id']))->setField('push_status',1);
				continue;
			}else{
				$failed_count++;
			}
		}
		
		$this->success($suc_count.'条成功！'.$failed_count.'条失败！');
	}
    //用户列表
    public function index(){
		$inbound=I('inbound');//入库单参数
        $keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$Product_model=M('ProductOrder',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Section_model=M('Section',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Personnel_model=M('Personnel',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        if(!empty($keywords)){
			if($selval=='1'){
				$map['purchasing_no'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Product_model->where($map)->count();
		$list=$Product_model->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=$Section_model->where(array('no'=>$v['section_no']))->find();
			$list[$k]['people_make_name']=$Personnel_model->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=$Personnel_model->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
			//$list[$k]['auditor_name']=M('User')->where(array('username'=>$v['auditor']))->find();//审核人
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("inbound",$inbound);
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	/*
	*入库
	*/
	public function inbound(){
		$warehouse_code=session('warehouse_code');
		$ProductOrder=M('ProductOrder');
		$this->getconfig();
		$conf=C('PICKCONFIG');
		$listrows=$conf['listrows']?$conf['listrows']:10;

		$keyword=I('keyword')?I('keyword'):0;
		$selval=I('selval')?I('selval'):0;
		
		$p=I('p')?I('p'):1;
		$firstrow=($p-1)*$listrows;
		$map['examined_status']=1;
		$map['pro_num']=array('gt',0);
		$map['warehouse_code']=$warehouse_code;
		if($keyword){
			if($selval=='1'){
				$map['pro_no']=array('like',"%$keyword%");
			}
			if($selval=='2'){
				$map['pro_name']=array('like',"%$keyword%");
			}
		}
		$count=$ProductOrder->where($map)->count();
		$list   = $ProductOrder->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		//echo $ProductOrder->getlastsql();
		//die;
		

		// echo $ProductOrder->getlastsql();
		$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show    = $Page->show();// 分页显示输出
		
		$this->assign('page',$show);// 赋值分页输出
		$this->assign("list",$list);
		$this->display();
    }
	
    //新增用户
    public function add(){
		$this->tslist();
		$m=M('UserWarehouse');
		$Warehouse=M('Warehouse');
		$warehouse_info=$m->where(array('user_id'=>session('agent.id')))->find();
		
		$warehouse_list=explode(',',$warehouse_info['warehouse_group_id']);
		// dump($warehouse_list);
		foreach($warehouse_list as $k=>$v){
			$warehouse_list[$k]=$Warehouse->where(array('id'=>$v))->find();
		}
		$this->assign('warehouse_list',$warehouse_list);
        if(IS_POST){
            $Product_model=M("ProductOrder", C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$purchasing_no=trim(I('purchasing_no'));
			$pro_name=trim(I('pro_name'));
			
			$pro_spec=trim(I('pro_spec'));
			$source_no=trim(I('source_no'));
			$pro_no=trim(I('pro_no'));
			
			$map['purchasing_no']=$purchasing_no;
			$status=$Product_model->where($map)->find();
			if($status){
				$this->error('生产计划单号重复');
			}
			$data['purchasing_no']=$purchasing_no;
			$data['source_no']=$source_no;
			$data['section_no']=trim(I('section_no'));
			$data['batch_no']=trim(I('batch_no'));
			$data['people_make_no']=trim(I('people_make_no'));
			$data['people_purchasing_no']=trim(I('people_purchasing_no'));
			
			$data['pro_name']=$pro_name;
			
			$data['pro_spec']=$pro_spec;
			$data['pro_no']=$pro_no;
			$data['batch_no']=trim(I('batch_no'));
			// $data['attach']=implode(',',I('img'));
			$data['pro_num_plan']=I('pro_num_plan');
			$data['unit1']=I('unit1');
			$data['inbound_warehouse_code']=I('inbound_warehouse_code');
			$data['mark']=I('mark');
			
			$data['product_weeks']=I('product_weeks');
			$data['product_marks']=I('product_marks');

			$data['add_time']=time();
			$res=$Product_model->add($data);
            if($res){
				$this->success('添加成功');
			}
        }else{
			$this->display();
		}
        

    }
	 /*
     * 编辑入库单商品价格
     */
    public function editor(){

        $param = I('param.','','trim');

        if(IS_POST){
			
            $Product_model = M('ProductOrder');

            $purchasing_no = $param['purchasing_no'];
			$id=$param['id'];
			
			$map['purchasing_no']=$purchasing_no;
			$map['id']=array('neq',$id);
			
            $plan_no =  $Product_model->where($map)->find();
			
            if($plan_no){
                $this->error('采购单号重复');
            }

           
            //$param['add_time'] = time();

			
			unset($param['strss']);
            $ress = $Product_model->where('id='.$id)->save($param);
			
            if($ress!==false){
				$str=explode(',',I('strss'));
				foreach($str as $k=>$v){
					$arr=explode(':',$v);
					$where['id']=$arr[0];
					$data['price']=$arr[1];
					$data['declare_amount']=$arr[2];
					$data['total_price']=$arr[1]*$arr[2];
					$data['weight']=$arr[3];
					$data['country_code']=$arr[4];
					$where['plan_id']=$param['id'];

					$status=M('ProductOrderDetail')->where($where)->save($data);
					//echo M('ProductOrderDetail')->getlastsql();
					
					
					if($status!==false){
						$res=$Product_model->where('id='.$id)->setField('add_goods',1);

						if($res!==false){
							continue;
						}else{
							$this->error('更新生产计划单添加原料状态出错');
						}
						
					}else{
						$this->error('修改失败');
					}
				}
				
                $this->success('生产计划单添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
			$id=$param['id'];
			
			$Productorderdetail_model = M('ProductOrderDetail');
			$country_model = M('country');
			$Product_model=M('ProductOrder');
			$coin_model = M('coin');
            $planPack_model = M('planPack');
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=$Product_model->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$this->getconfig();
			$conf=C('PICKCONFIG');
			$listrows=$conf['listrows']?$conf['listrows']:10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['plan_id']=$id;
			$count=$Productorderdetail_model->where($map)->count();
			$list   = $Productorderdetail_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出

			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
            }
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

        }

    }
	 /*
     * 编辑入库单、添加商品
     */
    public function edt_view(){
        $param = I('param.','','trim');
		$this->tslist();
		$m=M('UserWarehouse');
		$Warehouse=M('Warehouse');
		$warehouse_info=$m->where(array('user_id'=>session('agent.id')))->find();
		
		$warehouse_list=explode(',',$warehouse_info['warehouse_group_id']);
		// dump($warehouse_list);
		foreach($warehouse_list as $k=>$v){
			$warehouse_list[$k]=$Warehouse->where(array('id'=>$v))->find();
		}
		$this->assign('warehouse_list',$warehouse_list);
        if(IS_POST){
			if($param['for_id']==1){
				$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			}else{
				$Product_model=M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			}
			// dump($param);
			$id=$param['id'];
            $ress = $Product_model->where('id='.$id)->save($param);
            if($ress!==false){
				$res=$Product_model->where('id='.$id)->setField('add_goods',1);
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
			
			$id=$param['id'];
			$Productorderdetail_model=M('ProductOrderDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$b=M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model=M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model=M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$planPack_model=M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PurchasingInboundGoods=M('PurchasingInboundGoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$unit_model=M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			$detail=$Product_model->where('id='.$id)->find();
			$byproduct_list=$b->where(array('order_id'=>$id))->select();
			$this->assign("byproduct_list",$byproduct_list);
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			$attach_list=explode(',',$detail['attach']);
			foreach($attach_list as $k=>$v){
				$attach_list[$k]=explode('@@',$v);
			}

			$listrows=20;
			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;
			$map['m.plan_id']=$id;
			$count=$Productorderdetail_model->alias('m')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->count();
			$list = $Productorderdetail_model->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>2))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			// echo $Productorderdetail_model->getlastsql();
			
			$list_f = $Productorderdetail_model->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>3))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			// echo $Productorderdetail_model->getlastsql();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出

			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				$list[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
			foreach($list_f as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list_f[$k]['currency_name'] = $currency['coin_name'];
				$list_f[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list_f",$list_f);
			$this->assign("list",$list);
			$this->assign("country",$country);
			$this->assign('file_path',C('IMAGE_CONF11'));
			$this->assign('attach_list',$attach_list);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();
        }
    }
     /*
     * 领料单回填
     */
    public function edt(){
        $param = I('param.','','trim');
		$this->tslist();
		$m=M('UserWarehouse');
		$Warehouse=M('Warehouse');
		$Product_model=M('ProductOrder',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$warehouse_info=$m->where(array('user_id'=>session('agent.id')))->find();
		
		$warehouse_list=explode(',',$warehouse_info['warehouse_group_id']);
		// dump($warehouse_list);
		foreach($warehouse_list as $k=>$v){
			$warehouse_list[$k]=$Warehouse->where(array('id'=>$v))->find();
		}
		$this->assign('warehouse_list',$warehouse_list);
        if(IS_POST){
			$Productdetail_model = M('ProductOrderDetail',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ao=M('ArrivalOrder',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$id=$param['id'];
			$str=explode(',',I('str'));
			// dump($str);
				foreach($str as $k=>$v){
					$arr=explode(':',$v);
					$where['id']=$arr[0];
					$data['mark']=$arr[2];
					$data['weight']=$arr[1];

					$detail_info=$Productdetail_model->find($arr[0]);
					
					$ao_info=$ao->where(array('id'=>$detail_info['goods_id']))->find();
					$data['batch_no']=$ao_info['batch_no'];
					$data['total_price']=$arr[1]*$ao_info['price'];  //原料采购成本总价:单价*重量  单价的单位为每件原料1kg的价格
					$storage_nums=$ao_info['storage_amount'];//库存数量
					if($arr[1]>$storage_nums){
						$this->error('此数量大于库存数量不能保存');
					}
					$plan_no=$Product_model->where(array('id'=>$id))->find();
					//扣库存-begin
					if($plan_no['add_goods']==1){//已添加，则修改数据
						if($detail_info['weight']>$arr[1]){
							$weight=$detail_info['weight']-$arr[1];
							$ao->where(array('id'=>$detail_info['goods_id']))->save(array('freez_amount'=>array('exp','freez_amount-'.$weight),'storage_amount'=>array('exp','storage_amount+'.$weight)));//冻结库存
						}else{
							$weight=$arr[1]-$detail_info['weight'];
							$ao->where(array('id'=>$detail_info['goods_id']))->save(array('freez_amount'=>array('exp','freez_amount+'.$weight),'storage_amount'=>array('exp','storage_amount-'.$weight)));//冻结库存
						}
					}else{
						$ao->where(array('id'=>$detail_info['goods_id']))->save(array('freez_amount'=>array('exp','freez_amount+'.$arr[1]),'storage_amount'=>array('exp','storage_amount-'.$arr[1])));//冻结库存
					}
					//扣库存-end
					$result=$Productdetail_model->where($where)->save($data);
					if($result!==false){
						continue;	
					}else{
						$this->error('修改失败');
					}
				}
                $this->success('修改成功');

        }else{
			$id=$param['id'];
			$Productorderdetail_model=M('ProductOrderDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$b=M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model=M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model=M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$planPack_model=M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PurchasingInboundGoods=M('PurchasingInboundGoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$unit_model=M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=$Product_model->where('id='.$id)->find();
			$byproduct_list=$b->where(array('order_id'=>$id))->select();
			$this->assign("byproduct_list",$byproduct_list);
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$attach_list=explode(',',$detail['attach']);
			foreach($attach_list as $k=>$v){
				$attach_list[$k]=explode('@@',$v);
			}

			$listrows=20;
			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;
			$map['m.plan_id']=$id;
			$count=$Productorderdetail_model->alias('m')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->count();
			$list = $Productorderdetail_model->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.total_price,m.mark,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>2))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			$list_f = $Productorderdetail_model->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.total_price,m.mark,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>3))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			// dump($list);
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出

			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				$list[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
			foreach($list_f as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list_f[$k]['currency_name'] = $currency['coin_name'];
				$list_f[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list_f",$list_f);
			$this->assign("list",$list);
			$this->assign("country",$country);
			$this->assign('file_path',C('IMAGE_CONF11'));
			$this->assign('attach_list',$attach_list);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

        }

    }
	//最终确认
	public function lastSure($id){
        if($id==''){
            alert(0,'操作有误');
        }
        $g=session('agent');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');////因为对方现在有list_product表的id字段---gooods_id  所以不需要那么复杂

		if(I('sta')==1){//主产品
			$this->p = M('ProductOrder',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$result=$this->p->where("id='".$id."'")->find();
			$result['warehouse_code']=$result['inbound_warehouse_code'];
		}
		if(I('sta')==2){//副产品
			$this->pr = M('ProductOrder',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$this->p = M('ProductOrderByproduct',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$result=$this->p->where("id='".$id."'")->find();
			$result_info=$this->pr->where(array('id'=>$result['order_id']))->find();
			$result['warehouse_code']=$result_info['inbound_warehouse_code'];
		}
        $data=array(
            'erp_code'=>$g['company_id'],
            'goods_id'=>$result['id'],
            'is_last'=>true,
			'sta'=>I('sta'),
			'cate'=>'PRO'
        );

        ////根据参数找到对应的仓库
        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id",'left')
            ->where(C('DB_PREFIX')."warehouse.warehouse_code='".$result['warehouse_code']."'")
            ->getField('vars,url,warehouse_code');
        $url=$d['var_3']['url'];
        $warehouse_code=$d['var_3']['warehouse_code'];
        $data['warehouse_code']=$warehouse_code;
        $return =push($url,$data);
        $result=json_decode($return,true);
        if($result['status']==1){
            $datas['act_count']=$this->p->where("id='".$id."'")->getField('back_count');
            $this->p->where("id='".$id."'")->save($datas);
            alert(1,'成功');
        }else{
            alert(0,'失败');
        }
    }
	public function detailBack($id,$count){
        if($id=='' || $count==''){
            alert(0,'操作有误');
        }
        $g=session('agent');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');////因为对方现在有list_product表的id字段---gooods_id  所以不需要那么复杂
		if(I('sta')==1){//主产品
			$this->p = M('ProductOrder',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$result=$this->p->where("id='".$id."'")->find();
			$result['warehouse_code']=$result['inbound_warehouse_code'];
		}
		if(I('sta')==2){//副产品
			$this->pr = M('ProductOrder',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$this->p = M('ProductOrderByproduct',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$result=$this->p->where("id='".$id."'")->find();
			$result_info=$this->pr->where(array('id'=>$result['order_id']))->find();
			$result['warehouse_code']=$result_info['inbound_warehouse_code'];
		}
		// dump($result);
        $data=array(
            'erp_code'=>$g['company_id'],
            'goods_id'=>$result['id'],
            'count'=>$count,
			'sta'=>I('sta'),
			'cate'=>'PRO'
        );
        ////根据参数找到对应的仓库
        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id",'left')
            ->where(C('DB_PREFIX')."warehouse.warehouse_code='".$result['warehouse_code']."'")
            ->getField('vars,url,warehouse_code');
        $url=$d['var_2']['url'];
        $warehouse_code=$d['var_2']['warehouse_code'];
        $data['warehouse_code']=$warehouse_code;
		//dump($url);
        $return =json_decode(push($url,$data),true);
        if($return['status']){
            $data['is_back']=2;
            $data['back_count']=$count;
            $data['erp_code']=$g['company_id'];
            $true=$this->p->where("id='".$result['id']."'")->save($data);
            if($true){
                alert(1,'回执成功');
            }else{
                alert(0,'回执成功，更新状态失败');
            }
        }else{
            alert(0,'回执失败');
        }
    }
	public function receive_info(){
		$param = I('param.','','trim');
		$this->tslist();
		$m=M('UserWarehouse');
		$Warehouse=M('Warehouse');
		$warehouse_info=$m->where(array('user_id'=>session('agent.id')))->find();
		// dump($param);
		$warehouse_list=explode(',',$warehouse_info['warehouse_group_id']);
		// dump($warehouse_list);
		foreach($warehouse_list as $k=>$v){
			$warehouse_list[$k]=$Warehouse->where(array('id'=>$v))->find();
		}
		$this->assign('warehouse_list',$warehouse_list);
        if(IS_POST){
			if($param['for_id']==1){
				$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			}else{
				$Product_model=M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			}
			$Productdetail_model = M('ProductOrderDetail',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ao=M('ArrivalOrder',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$id=$param['id'];
			
			$param['overdue_time']=strtotime($param['overdue_time']);
			$param['factory_time']=strtotime($param['factory_time']);
			$ress = $Product_model->where('id='.$id)->save($param);
			if($ress!==false){
				$this->success('修改成功');
			}else{
				$this->success('修改失败');
			}
        }else{
			$id=$param['id'];
			$Productorderdetail_model=M('ProductOrderDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$b=M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model=M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model=M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$planPack_model=M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PurchasingInboundGoods=M('PurchasingInboundGoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$unit_model=M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=$Product_model->where('id='.$id)->find();
			
			$byproduct_list=$b->where(array('order_id'=>$id))->select();
			
			$this->assign("byproduct_list",$byproduct_list);
			
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$attach_list=explode(',',$detail['attach']);
			foreach($attach_list as $k=>$v){
				$attach_list[$k]=explode('@@',$v);
			}

			$listrows=20;
			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;
			$map['m.plan_id']=$id;
			$count=$Productorderdetail_model->alias('m')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->count();
			$list = $Productorderdetail_model->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.total_price,m.mark,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>2))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			$list_f = $Productorderdetail_model->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.total_price,m.mark,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>3))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			// dump($list);
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出

			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				$list[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
			foreach($list_f as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list_f[$k]['currency_name'] = $currency['coin_name'];
				$list_f[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list_f",$list_f);
			$this->assign("list",$list);
			$this->assign("country",$country);
			$this->assign('file_path',C('IMAGE_CONF11'));
			$this->assign('attach_list',$attach_list);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();
        }
    }
	/*
     * 转换单回填
     */
    public function edt_change(){
		$param = I('param.','','trim');
		$this->tslist();
		$m=M('UserWarehouse');
		$Warehouse=M('Warehouse');
		$warehouse_info=$m->where(array('user_id'=>session('agent.id')))->find();
		// dump($param);
		$warehouse_list=explode(',',$warehouse_info['warehouse_group_id']);
		// dump($warehouse_list);
		foreach($warehouse_list as $k=>$v){
			$warehouse_list[$k]=$Warehouse->where(array('id'=>$v))->find();
		}
		$this->assign('warehouse_list',$warehouse_list);
        if(IS_POST){
			if($param['for_id']==1){
				$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			}else{
				$Product_model=M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			}
			$Productdetail_model = M('ProductOrderDetail',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ao=M('ArrivalOrder',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$id=$param['id'];
			
			$param['overdue_time']=strtotime($param['overdue_time']);
			$param['factory_time']=strtotime($param['factory_time']);
			$ress = $Product_model->where('id='.$id)->save($param);
			if($ress!==false){
				$this->success('修改成功');
			}else{
				$this->success('修改失败');
			}
        }else{
			$id=$param['id'];
			$Productorderdetail_model=M('ProductOrderDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$b=M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model=M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model=M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$planPack_model=M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$PurchasingInboundGoods=M('PurchasingInboundGoods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$unit_model=M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=$Product_model->where('id='.$id)->find();
			
			$byproduct_list=$b->where(array('order_id'=>$id))->select();
			
			$this->assign("byproduct_list",$byproduct_list);
			
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$attach_list=explode(',',$detail['attach']);
			foreach($attach_list as $k=>$v){
				$attach_list[$k]=explode('@@',$v);
			}

			$listrows=20;
			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;
			$map['m.plan_id']=$id;
			$count=$Productorderdetail_model->alias('m')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->count();
			$list = $Productorderdetail_model->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.total_price,m.mark,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>2))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			$list_f = $Productorderdetail_model->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.total_price,m.mark,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>3))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			// dump($list);
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出

			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				$list[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
			foreach($list_f as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list_f[$k]['currency_name'] = $currency['coin_name'];
				$list_f[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list_f",$list_f);
			$this->assign("list",$list);
			$this->assign("country",$country);
			$this->assign('file_path',C('IMAGE_CONF11'));
			$this->assign('attach_list',$attach_list);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();
        }
    }
	public function edt_change_p(){
			$param = I('param.','','trim');
			$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Productdetail_model = M('ProductOrderDetail',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ao=M('ArrivalOrder',C('DB_PREFIX_T'), C('DB_TYPE').'://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$id=$param['id'];
			
			$str=explode(',',I('str'));
				foreach($str as $k=>$v){
					$arr=explode(':',$v);
					$where['id']=$arr[0];
					$data['scrap_weight']=$arr[2];
					$data['material_weight']=$arr[1];

					$detail_info=$Productdetail_model->find($arr[0]);
					$ao_info=$ao->where(array('id'=>$detail_info['goods_id']))->find();
					$data['batch_no']=$ao_info['batch_no'];
					$data['total_price']=$arr[1]*$ao_info['price'];  //原料采购成本总价:单价*重量  单价的单位为每件原料1kg的价格
					$storage_nums=$ao_info['storage_amount'];//库存数量
					if($data['weight']>$storage_nums){
						$this->error('此数量大于库存数量不能保存');
					}
					$result=$Productdetail_model->where($where)->save($data);
					if($result!==false){
						continue;	
					}else{
						$this->error('修改失败');
					}
				}
                $this->success('修改成功');
    }
	//采购单审批
	public function examined(){
		$warehouse_code=session('warehouse_code');
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$Product_model=M('ProductOrder');
		
		$map['warehouse_code']=$warehouse_code;
		$map['add_goods']=1;//为1时确定已经添加过商品
        if(!empty($keywords)){
			if($selval=='1'){
				$map['purchasing_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['auditor'] = array('like','%'.$keywords.'%');
			}
        }
		
		$count=$Product_model->where($map)->count();
		$list=$Product_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=M('Section')->where(array('no'=>$v['section_no']))->find();

			$list[$k]['people_make_name']=M('Personnel')->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=M('Personnel')->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
			$list[$k]['auditor_name']=M('User')->where(array('username'=>$v['auditor']))->find();//审核人
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
		
	}
	/*
     * 审核
     */
	public function examinedset(){
		// dump(session());
		$data['status']=I('examined_status');
		$data['auditor']=session('agent.name');
		$data['examine_mark']=I('mark');
		$data['examined_time']=time();
		$id=I('id');
		$str=I('str');
		
		$ProductOrder=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		if(I('examined_status')==0){
			$this->error('请选择审核状态');
			exit;
		}
		$map['id']=$id;

		$res=$ProductOrder->where($map)->save($data);
		if($res!==false){
			$this->success('审核状态更改成功');
		}
	}
	public function examinedset_picking(){
		// dump(session());
		$data['picklist_examined_status']=I('examined_status');
		$data['picking_auditor']=session('agent.name');
		$data['picking_examine_mark']=I('mark');
		$data['picklist_examine_time']=time();
		$id=I('id');
		$str=I('str');
		
		$ProductOrder=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		if(I('examined_status')==0){
			$this->error('请选择审核状态');
			exit;
		}
		$map['id']=$id;

		$res=$ProductOrder->where($map)->save($data);
		if($res!==false){
			$this->success('审核状态更改成功');
		}
	}
	public function examinedset_changelist(){
		// dump(session());
		$data['changelist_examined_status']=I('examined_status');
		$data['changelist_auditor']=session('agent.name');
		$data['changelist_examine_mark']=I('mark');
		$data['changelist_examine_time']=time();
		$id=I('id');
		$str=I('str');
		
		$ProductOrder=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		if(I('examined_status')==0){
			$this->error('请选择审核状态');
			exit;
		}
		$map['id']=$id;

		$res=$ProductOrder->where($map)->save($data);
		if($res!==false){
			$this->success('审核状态更改成功');
		}
	}
	 /*
     * 原料生产单
     */
    public function prorder(){
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$Product_model=M('ProductOrder');
		$map['examined_status']=1;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['purchasing_no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['auditor'] = array('like','%'.$keywords.'%');
			}
			$count=$Product_model->where($map)->count();
			$list=$Product_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			
        }else{
			$count=$Product_model->where($map)->count();
			$list=$Product_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		}
		
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();         
	}
	
	
	 /*
     * 打印计划生产单
     */
    public function print_prorder(){

        $param = I('param.','','trim');

			$id=$param['id'];
			
			$ProductOrderDetail = M('ProductOrderDetail');
			$country_model = M('country');
			$ProductOrder=M('ProductOrder');
			$coin_model = M('coin');
            $planPack_model = M('planPack');
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('ProductOrder')->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$this->getconfig();
			$conf=C('PICKCONFIG');
			$listrows=$conf['listrows']?$conf['listrows']:10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['plan_id']=$id;
			$count=$ProductOrderDetail->where($map)->count();
			$list   = $ProductOrderDetail->where($map)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
		
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				
				$list[$k]['goods_name']=M('PurchasingGoods')->where(array('goods_no'=>$v['goods_no']))->find();
            }
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

     

    }
	 /*
     * 打印生产计划单
     */
    public function print_examinedetail(){
        $param = I('param.','','trim');

			$id=$param['id'];
			
			$ProductOrderDetail = M('ProductOrderDetail');
			$country_model = M('country');
			$ProductOrder=M('ProductOrder');
			$coin_model = M('coin');
            $planPack_model = M('planPack');
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('ProductOrder')->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$this->getconfig();
			$conf=C('PICKCONFIG');
			$listrows=$conf['listrows']?$conf['listrows']:10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['plan_id']=$id;
			$count=$ProductOrderDetail->where($map)->count();
			$list   = $ProductOrderDetail->where($map)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
		
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				
				$list[$k]['goods_name']=M('PurchasingGoods')->where(array('goods_no'=>$v['goods_no']))->find();
            }
			
			$attach_list=explode(',',$detail['attach']);//附件列表
			foreach($attach_list as $k=>$v){
				$attach_list[$k]=explode('@@',$v);
			}
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("attach_list",$attach_list);
			$this->assign('file_path',C('IMAGE_CONF11'));
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

     

    }
	 /*
     * 打印领料单
     */
    public function print_examinedetailpicking(){

        $param = I('param.','','trim');

			$id=$param['id'];
			
			$ProductOrderDetail = M('ProductOrderDetail');
			$country_model = M('country');
			$ProductOrder=M('ProductOrder');
			$coin_model = M('coin');
            $planPack_model = M('planPack');
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('ProductOrder')->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$this->getconfig();
			$conf=C('PICKCONFIG');
			$listrows=$conf['listrows']?$conf['listrows']:10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['plan_id']=$id;
			$count=$ProductOrderDetail->where($map)->count();
			$list   = $ProductOrderDetail->where($map)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			
		
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				
				$list[$k]['goods_name']=M('PurchasingGoods')->where(array('goods_no'=>$v['goods_no']))->find();
            }
			
			$attach_list=explode(',',$detail['attach']);//附件列表
			foreach($attach_list as $k=>$v){
				$attach_list[$k]=explode('@@',$v);
			}
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("attach_list",$attach_list);
			$this->assign('file_path',C('IMAGE_CONF11'));
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();

     

    }
	 /*
     * 打印转换单
     */
    public function print_examinedetailchange(){

			$param = I('param.','','trim');

			$id=$param['id'];
			
			$ProductOrderDetail = M('ProductOrderDetail');
			$country_model = M('country');
			$ProductOrder=M('ProductOrder');
			$coin_model = M('coin');
            $planPack_model = M('planPack');
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=M('ProductOrder')->where('id='.$id)->find();
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();
			
			$this->getconfig();
			$conf=C('PICKCONFIG');
			$listrows=$conf['listrows']?$conf['listrows']:10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['plan_id']=$id;
			$count=$ProductOrderDetail->where($map)->count();
			$list   = $ProductOrderDetail->where($map)->order('id desc')->select();
			
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['currency']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				
				$list[$k]['goods_name']=M('PurchasingGoods')->where(array('goods_no'=>$v['goods_no']))->find();
				$list[$k]['mixedinfo']=M('MixedGoods')->where(array('goods_no'=>$v['mixed_no']))->find();
            }
			
			$attach_list=explode(',',$detail['attach']);//附件列表
			foreach($attach_list as $k=>$v){
				$attach_list[$k]=explode('@@',$v);
			}
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("attach_list",$attach_list);
			$this->assign('file_path',C('IMAGE_CONF11'));
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
            $this->display();
    }
	 /*
     * 编辑入库单、添加商品
     */
    public function examinedetail(){
			$this->tslist();
			$m=M('UserWarehouse');
			$Warehouse=M('Warehouse');
			$warehouse_info=$m->where(array('user_id'=>session('agent.id')))->find();
			
			$warehouse_list=explode(',',$warehouse_info['warehouse_group_id']);

			foreach($warehouse_list as $k=>$v){
				$warehouse_list[$k]=$Warehouse->where(array('id'=>$v))->find();
			}
			$this->assign('warehouse_list',$warehouse_list);
        $param = I('param.','','trim');
			$id=$param['id'];
			$ProductOrderDetail = M('ProductOrderDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$b = M('ProductOrderByproduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ProductOrder=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$unit_model = M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $planPack_model = M('planPack', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $plan_pack = $planPack_model->select();
			$country = $country_model->select();
			
			$detail=$ProductOrder->where('id='.$id)->find();
			$byproduct_list=$b->where(array('order_id'=>$id))->select();
			$this->assign("byproduct_list",$byproduct_list);
			$detail['wrap_type_name']=$planPack_model->where('code='.$detail['wrap_type'])->find();

			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['m.plan_id']=$id;
			$count=$ProductOrderDetail->alias('m')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->count();
			$list = $ProductOrderDetail->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.total_price,m.mark,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>2))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			$list_f = $ProductOrderDetail->alias('m')->field('m.id,m.plan_id,m.goods_id,m.scrap_weight,m.material_weight,m.outbound_warehouse_code,m.batch_no,m.mark,n.goods_no,n.manual_id as manua_id,n.source_no,n.goods_name,n.goods_spec,n.currency as current_type,n.country_code,n.unit1,n.price,n.batch_no,m.total_price,m.mark,m.weight,m.add_time,m.status')->join('__ARRIVAL_ORDER__ n on m.goods_id=n.id')->where($map)->where(array('m.goods_cate'=>3))->limit($firstrow.','.$listrows)->order('m.id desc')->select();
			// dump($list);
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出

			foreach($list as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list[$k]['currency_name'] = $currency['coin_name'];
				$list[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
			foreach($list_f as $k=>$v){
                $currency =$coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$unit1_name =$unit_model->where(array('unit_no'=>$v['unit1']))->find();
                $list_f[$k]['currency_name'] = $currency['coin_name'];
				$list_f[$k]['unit1_name'] = $unit1_name['unit_name'];
            }
			
			$attach_list=explode(',',$detail['attach']);//附件列表
			foreach($attach_list as $k=>$v){
				$attach_list[$k]=explode('@@',$v);
			}
			
            $data['plan_pack']      = $plan_pack;
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list_f",$list_f);
			$this->assign("list",$list);
			$this->assign("country",$country);
			$this->assign('file_path',C('IMAGE_CONF11'));
			$this->assign('attach_list',$attach_list);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
			if(I('cate')=='picking'){
				$this->display('examinedetail_picking');
			}else if(I('cate')=='changelist'){
				$this->display('examinedetail_changelist');
			}else{
				$this->display();
			}
           
    }
	/*
     * 商品列表
     */
    public function goods(){
			$ao = M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$listrows=10;

			$keyword=I('keyword')?I('keyword'):0;
			$plan_id=I('plan_id')?I('plan_id'):0;
			$id=I('id');
			$now_plan_id=$plan_id?$plan_id:$id;
			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;
			
			if($keyword){
				$map['ao.goods_no']=array('like',"%$keyword%");
			}
			$goods_cate=I('goods_cate');
			if(!empty($goods_cate)){
				$map['p.goods_cate']=$goods_cate;
			}

			$count=$ao->alias('ao')->join('__PRODUCT__ p on ao.goods_no=p.goods_no')->where($map)->count();
			$list = $ao->alias('ao')->field('ao.*')->join('__PRODUCT__ p on ao.goods_no=p.goods_no')->where($map)->limit($firstrow.','.$listrows)->order('ao.id desc')->select();
			// echo $ao->getlastsql();
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            $unit_model =  M('unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
            foreach($list as $k=>$v){

                $current_type = $coin_model->where(array('coin_code'=>$v['currency']))->find();
                $unit = $unit_model->where(array('unit_no'=>$v['declare_unit']))->find();
                if($v['unit1']){
                    $unit1 = $unit_model->where(array('unit_no'=>$v['unit1']))->find();
                }
                if($v['unit2']){
                    $unit2 = $unit_model->where(array('unit_no'=>$v['unit2']))->find();
                }
                $list[$k]['coin_name']   =  $current_type['coin_name'];
                $list[$k]['unit_name']   =  $unit['unit_name'];
                $list[$k]['unit1_name']  =  $unit1['unit_name'];
                $list[$k]['unit2_name']  =  $unit2['unit_name'];
            }
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出
			
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
            $this->assign("plan_id",$now_plan_id);
            $this->display();
    }
	/*
     * 添加商品
     */
    public function add_goods(){
		$param = I('param.','','trim');
		if(!$param['ids']){
            $this->error("请选择商品");
        }
        $goods_ids = $param['ids'];
        $plan_id = $param['plan_id'];

        $ProductOrder = M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        $ProductOrderDetail = M('ProductOrderDetail', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));

        $plan_no = $ProductOrder->find($plan_id);
		$error=0;
        foreach($goods_ids as $k=>$v){
            $goods = M('ArrivalOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->find($v);
			
            $plan_goods = array();
            $plan_goods['plan_id'] = $plan_id;
            $plan_goods['goods_id'] = $v;
            $plan_goods['goods_cate']=$param['goods_cate'];

            $where = array();
            $where['plan_id'] = $plan_id;
            $where['goods_id'] = $v;

            $exit = $ProductOrderDetail->where($where)->find();

            if($exit){
                $error++;
            }else{
                $ProductOrderDetail->add($plan_goods);
            }
        }

        if($error){
            $mes = "有".$error.'条数据添加失败 其他的添加成功';
            $this->error($mes);
        }else{
			$ProductOrder->where(array('id'=>$plan_id))->save(array('add_goods'=>1));
            $this->success("成功");
        }
	}



    //删除
    public function del(){
		$m=M("ProductOrder", C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        $id=I('post.id');
        $res=$m->where("id='$id'")->delete();
        if($res!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
    }

   /**
	*编辑商品
	**/
	public function editgood() {
		$goods_id=I('goods_id');
		$data['pro_no']=I('pro_no');
		$data['receive_amount']=I('receive_amount');
		$status=M('ProductOrder')->where('id='.$goods_id)->save($data);
		if($status!==false){
			$this->success('修改成功');
		}else{
			$this->error('修改失败，请联系管理员！');
		}
		
	}
	/**
	*详细商品列表
	**/
	public function goodsdetail(){
		$id=I('id')?I('id'):0;
		$view=I('view');
		$country =  M('Country');
		$unit_model =  M('Unit');
		$coin_model = M('Coin');
		$country_list	=	$country->select();
		$unit_list	=	$unit_model->select();
		$coin_list	=	$coin_model->select();
		
		$this->assign('country_list',$country_list);
		$this->assign('unit_list',$unit_list);
		$this->assign('coin_list',$coin_list);
		if($id){
			$Goods=M('ProductOrder');
			$detail=$Goods->where("id=$id")->find();

			$this->assign("detail",$detail);
		}else{
			$this->error('请关联商品id');
		}
		if($view==1){
			$this->display('goodsdetailview');
		}else{
			$this->display();
		}
		
	}

	/**
	*入库操作
	**/
	public function grounding(){
		$warehouse_code=session('warehouse_code');
		$goods=M('Goods');
		$ProductOrder=M('ProductOrder');
		$param = I('param.','','trim');
		$id=$param['goods_id'];
		
		$pro_no=$param['pro_no'];

		$receive_amount=$param['receive_amount'];

		$goods_num=$receive_amount;//实际入库数量

		
		unset($param['goods_id']);
		$status=M('ProductOrder')->where('id='.$id)->save($param);

		if($id){
			
			$res=$ProductOrder->where("id='$id' and pro_no='$pro_no'")->setField('inbound_status',1);
			
			$map['goods_no']=$pro_no;
			
			$status=$goods->where($map)->find();
			
			if($status){//如果已经存在此条数据，更新库存信息
				$data['amount']=array('exp',"amount+$goods_num");
				$ress=$goods->where($map)->save($data);
				if($ress!==false){
					$this->success('更新商品入库信息成功！');
				}else{
					$res=$ProductOrder->where("id='$id' and pro_no='$pro_no'")->setField('inbound_status',0);
					$this->error('更新入库信息失败！');
				}
			}else{//不存在此条数据，则添加商品信息
				
				$list=$ProductOrder->where("id='$id'")->find();
				
				$data['source_no']=$list['source_no'];
				$data['goods_name']=$list['pro_name'];
				$data['goods_no']=$list['pro_no'];
				$data['amount']=$goods_num;
				$data['inbound_price']=$list['inbound_price']*$list['pro_num']/$goods_num;//入库成本价：生产成本价*生产数量/实际入库数量
				$data['material_cost_price']=$list['material_cost_price'];
				$data['sell_price']=$list['sell_price'];
				$data['factory_price']=$list['inbound_price'];
				
				$data['warehouse_code']=$warehouse_code;
				$data['manua_id']=$list['manua_id'];
				$data['item_no']=$list['item_no'];
				$data['item_type']=$list['item_type'];
				$data['goods_spec']=$list['pro_spec'];
				$data['current_type']=$list['current_type'];
				$data['declare_unit']=$list['declare_unit'];
				$data['unit1']=$list['unit1'];
				$data['unit2']=$list['unit2'];
				$data['factory_time']=$list['factory_time'];
				$data['overdue_time']=$list['overdue_time'];
				
				$resss=$goods->add($data);
				// echo $goods->getlastsql();
				// die;
				
				if($resss){
					$this->success('添加商品入库信息成功！');
				}else{
					$res=$ProductOrder->where("id='$id' and pro_no='$pro_no'")->setField('inbound_status',0);
					$this->error('添加入库信息失败！');
				}
			}

		}else{
			$this->error('请选择商品！');
		}
	}
	
	/**
	*下架操作
	**/
	public function ungrounding() {
		$goods=D('ProductOrderDetail');
		$id=I('goods_id');
		if($id){
			$data['status']=0;
			$data['storage_id']=0;
			$status=$goods->where('id='.$id)->save($data);
			if($status!==false){
				$this->success('商品下架成功！');
			}
		}else{
			$this->error('请选择商品！');
		}
		
	}
	
	/**
	*领料单
	**/
	public function picking(){
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Section_model=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Personnel_model=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));

		$map['status']=1;
		
		if($selval=='1'){
			$map['purchasing_no'] = array('like','%'.$keywords.'%');
		}

		$count=$Product_model->where($map)->count();
		$list=$Product_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=$Section_model->where(array('no'=>$v['section_no']))->find();

			$list[$k]['people_make_name']=$Personnel_model->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=$Personnel_model->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();
	}
	/**
	*领料单
	**/
	public function change(){
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		$Product_model=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Section_model=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Personnel_model=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));

		$map['picklist_examined_status']=1;
		
		if($selval=='1'){
			$map['purchasing_no'] = array('like','%'.$keywords.'%');
		}

		$count=$Product_model->where($map)->count();
		$list=$Product_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['section_name']=$Section_model->where(array('no'=>$v['section_no']))->find();

			$list[$k]['people_make_name']=$Personnel_model->where(array('no'=>$v['people_make_no']))->find();//制单人
			$list[$k]['people_purchasing_name']=$Personnel_model->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
				
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();
	}
	//生产计划单常用列表
	public function tslist(){
		$Unit=M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ProductOrder=M('ProductOrder', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$unit_list=$Unit->select();
		$section_list=$Section->select();
		$personnel_list=$Personnel->select();
		$company_list=$ContactsCompany->select();
		//当日流水号生成
		$map['_string']="add_time-UNIX_TIMESTAMP(DATE(NOW()))>0 and add_time-UNIX_TIMESTAMP(DATE(NOW()))<86400";
		$count=$ProductOrder->where($map)->count();//当天添加的采购单数
		//echo $ProductOrder->getlastsql();
		$count=$count+1;
		$serial_number="";
		if($count<10){
			$serial_number='0000'.strval($count);
		}
		if($count>=10 and $count<100){
			$serial_number='000'.$count;
		}
		if($count>=100 and $count<1000){
			$serial_number='00'.$count;
		}
		if($count>=1000 and $count<10000){
			$serial_number='0'.$count;
		}
		$this->assign('unit_list',$unit_list);
		$this->assign('serial_number',$serial_number);//流水序号
		$this->assign('section_list',$section_list);
		$this->assign('personnel_list',$personnel_list);
		$this->assign('company_list',$company_list);
	}
    //and so on...
}